<?php
class Zendvn_Models_CategoryBusiness extends Zend_Db_Table{
	protected $_name = 'business_category';
	protected $_primary = 'id';
	
	public function itemInSelectboxx($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		if($options == null){
			$select = $db->select()
						 ->from($this->_name . ' AS c', array('id','name'));
			$result = $db->fetchPairs($select)	;
			$result[0] = ' -- Select an Item -- ';
			ksort($result);								 
		}
		return $result;
	}
	public function countItem($arrParam = null, $options = null){
		$db = $this->getAdapter();
		$ssFilter = $arrParam['ssFilter'];		
		$select = $db	->select()
						->from($this->_name . ' AS c',array('COUNT(c.id) AS totalItem'));		
		if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('c.name LIKE ?',$keywords,STRING);
		}
		$result = $db->fetchOne($select);
		return $result;		
	}
	
	public function sortItem($arrParam = null, $options = null){			
		$items = $arrParam[$options['column']];
		if(count($items)>0){
			foreach ($items as $key => $val) {
				$where = 'id = ' . $key;				
				$data = array($options['column']=>$val);
				$this->update($data,$where);
			}
		}
	}
	
	public function listItem($arrParam = null, $options = null){
		$ssFilter = $arrParam['ssFilter'];
		$db	= $this->getAdapter();
		$position = $options['position'];
		if($options['task'] == 'admin-list'){
			$select = $db	->select()
							->from($this->_name . ' AS c')
							->joinLeft("users AS u","u.id = c.created_by",array("u.user_name AS created_by"));							
			// Order..............
			$order = 'id DESC';
			$ssFilter = $arrParam['ssFilter'];
			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])){
				$order = $ssFilter['col'] . ' ' . $ssFilter['order'];
			}
			$select->order($order);
						
			//Filter
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('c.name LIKE ?',$keywords,STRING);
			}
			$result  = $db->fetchAll($select);
			$recursive = new Zendvn_System_Recursive($result);
			$result = $recursive -> buildArray(0);
		}
		if($options['task'] == 'block-category'){
			$select = $db	->select()
							->from($this->_name . ' AS c',array('id','name','alias','status','picture','order','created','parents'))
							->joinLeft("users AS u","u.id = c.created_by",array("u.user_name AS created_by"))
							->where('parents=0');							
			// Order..............
			$order = 'order ASC';
			$select->order($order);
			//$select->where('id = 129');
			$result  = $db->fetchAll($select);
			$recursive = new Zendvn_System_Recursive($result);
			$result = $recursive -> buildArray(0);
		}
		if($options['task'] == 'find-category'){
			$cid = $arrParam['tid'];
			$select = $db	->select()
							->from($this->_name . ' AS c',array('id','name'))
							->where('id=?',$cid);
			$result  = $db->fetchRow($select);		
		}
		
		if($options['task'] == 'public'){
			$select = $db	->select()
							->from($this->_name . ' AS c')
							->joinLeft("users AS u","u.id = c.created_by",array("u.user_name AS created_by"))
							->where('parents = 0')
							->where('c.status = 1');
							//->where('c.positions = ? OR c.positions = 0',$position);
			$order = 'order ASC';
			$select->order($order);
			$result  = $db->fetchAll($select);
		}
		if($options['task'] == 'block-child'){
			$select = $db	->select()
							->from($this->_name . ' AS c',array('id','name','alias','status','picture','order','created','parents','positions'))
							->where('parents = ?',$options['parents'])
							->where('c.status = 1')
							->where('c.positions = ? OR c.positions = 0',$position);
									
			$order = 'order ASC';
			$select->order($order);
			$result  = $db->fetchAll($select);
		}
		return $result;	
	}	
	public function saveItem($arrParam = null, $options = null){
		$filter = new Zend_Filter();
		$multiFilter = $filter	->addFilter(new Zend_Filter_StringToLower(array('encoding'=>'UTF-8')))
								->addFilter(new Zend_Filter_StringTrim())
						 		->addFilter(new Zend_Filter_Alnum(true))
//						  		->addFilter(new Zend_Filter_PregReplace(array('match'=>'#\s+#','replace'=>'-')))
						  		->addFilter(new Zend_Filter_Word_SeparatorToDash())
						  		->addFilter(new Zendvn_Filter_RemoveCircumflex());
						  		
		$info = new Zendvn_System_Info();
		$memberInfo = $info->getMemberInfo('id');
		if($options['task'] 	== 'admin-add'){
			$row 				= $this->fetchNew();
			$row->created		= date("Y-m-d-G-i-s");
			$row->created_by 	= $memberInfo;	
		}		
		if($options['task'] == 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];			
			$row =  $this->fetchRow($where);
			$row->modified 		= date("Y-m-d-G-i-s");
			$row->modified_by 	= $memberInfo;
		}
		$row->name 			= $arrParam['name'];
		$row->parents 		= $arrParam['parents'];
		if(!empty($arrParam['picture'])):$row->picture	= $arrParam['picture'];endif;
		$row->status 			= $arrParam['status'];
		$row->order 			= $arrParam['order'];
		$row->description_html 	= $arrParam['description_html'];
		$row->keywords_html 	= $arrParam['keywords_html'];
		if(empty($arrParam['alias'])){
			$row->alias 		= $filter->filter($arrParam['name']);
		}else{
			$row->alias = $arrParam['alias'];
		}
		$row->save();	
	}
	
	public function getItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
		}
		if($options['task'] == 'admin-info'){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from($this->_name . ' AS c',array('id','name','status','order','parents','created_by','picture'))
						 ->joinLeft('business_category AS c2','c.parents = c2.id',array('name as parent_name'))
						 ->order('c.order ASC')
						 ->where('c.id =?',$arrParam['id']);
			$result = $db->fetchRow($select);
		}
		return $result;
	}	
	public function deleteItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-delete'){
			$db = Zend_Registry::get('connectDb');
			$id = $arrParam['id'];
			$select = $db->select()
						 ->from($this->_name . ' AS c',array('id','name','picture','status','order','parents','created_by'));
			$result  = $db->fetchAll($select);
			$recursive = new Zendvn_System_Recursive($result);
			$result = $recursive->buildArray($arrParam['id']);
			$selectParent = $db->select()
						 ->from($this->_name . ' AS c',array('id','picture'))
						 ->where('id = ?',$arrParam['id']);
			$parent = $db->fetchRow($selectParent);
			array_unshift($result,array('id'=>$arrParam['id'],'picture'=>$parent['picture']));
			foreach ($result as $key=>$value){			
				$where = 'id = ' .$value['id'];
				$this->delete($where);
				$upload_dir = FILES_PATH . '/category';
				$upload = new Zendvn_File_Upload();
				$upload->removeFile($upload_dir . '/orignal/' . $value['picture']);
				$upload->removeFile($upload_dir . '/images100x100/' . $value['picture']);
				$upload->removeFile($upload_dir . '/images450x450/' . $value['picture']);
			}			
		}		
		if($options['task'] == 'admin-multi-delete'){			
			$db = Zend_Registry::get('connectDb');
			$cid = $arrParam['cid'];
			$select = $db->select()
						 ->from($this->_name . ' AS c',array('id','name','picture','status','order','parents','created_by'));
			$result  = $db->fetchAll($select);
			$newArr = array();
			foreach ($cid as $key1 => $val1){				
				$id = $val1;
				$newArr[] = array('id'=>$id);
				$recursive = new Zendvn_System_Recursive($result);
				$tmp = $recursive->buildArray($id);	
				if(count($tmp)>0){
					foreach ($tmp as $keyTmp => $valTmp){
						$newArr[] = $valTmp;							
					}
				}
			}
			$ids = '(';
			foreach ($newArr as $key => $value){				
				$ids .= $value['id'] . ',';
			}
			$ids .= '0)';
			$selectInfo = $db->select()
						 ->from($this->_name . ' AS c',array('id','picture'))
						 ->where('id IN' . $ids);
			$infoDelete = $db->fetchAll($selectInfo);
			echo $selectInfo;
			foreach ($infoDelete as $keyNew => $valNew){
				$id = $valNew['id'];
				$where = 'id = ' .$id;
				$this->delete($where);
				$upload_dir = FILES_PATH . '/category';
				$upload = new Zendvn_File_Upload();
				$upload->removeFile($upload_dir . '/orignal/' . $valNew['picture']);
				$upload->removeFile($upload_dir . '/images100x100/' . $valNew['picture']);
				$upload->removeFile($upload_dir . '/images450x450/' . $valNew['picture']);
			}			
		}
	}
	public function changeStatus($arrParam = null, $options = null){
		$cid = $arrParam['cid'];		
		if(count($cid)>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}			
			$ids = implode(',',$cid);
			$data = array('status'=>$status);
			$where = 'id IN (' . $ids . ')';
			$this->update($data,$where);
		}
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}			
			$data = array('status'=>$status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data,$where);
		}		
	}
	public function changeAcp($arrParam = null, $options = null){
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$group_acp = 1;
			}else{
				$group_acp = 0;
			}			
			$data = array('group_acp'=>$group_acp);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data,$where);
		}
	}
	public function itemInSelectbox($arrParam = null, $options = null){
		$db = $this->getAdapter();
		if($options == null){
			$select = $db->select()
						 ->from($this->_name . ' AS c',array('id','name','status','order','parents','created_by'))
						 ->order('c.order ASC');
			$result  = $db->fetchAll($select);		 
		}
		if($options['task'] == "admin-edit"){
			$id = $arrParam['id']; 
			$select = $db->select()
						 ->from($this->_name . ' AS c',array('id','name','status','order','parents','created_by'))
						 ->order('c.order ASC')
						 ->where('id !=?',$id,INTEGER);
			$result  = $db->fetchAll($select);
		}		
		$recursive = new Zendvn_System_Recursive($result);
		$result = $recursive -> buildArray(0);
		$tmp = array('id'=>0,'parents'=>0,'level'=>1,'name'=>'Root Category');		
		array_unshift($result, $tmp);
		return $result;
	}	
}